/**
 * @projectName practise
 * @package com.wzl.base
 * @className com.wzl.base.Main
 * @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
 */
package com.wzl.base;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;
import java.util.regex.Pattern;

import com.wzl.base.domain.Student;
import com.wzl.model.Xzqh;
import com.wzl.util.IdcardUtils;

/**
 * Main
 * @description
 * @author weiZhiLin
 * @date 2020/12/25 14:27
 * @version 1.0
 */
public class Main {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
        // 测试 main 方法传参的作用
        // 参考资料 -》 https://blog.csdn.net/alan_gaohaodong/article/details/78837321
//        System.out.println(args);
//        for (String arg : args) {
//            System.out.println(arg);
//            System.out.println("---");
//        }
//        fieldTest();
//        scannerTest();
//        testBufferedReader();
//        checkIDcard();
//        checkPhone();
//        testBufferedReader2();
//        System.out.println(4 / 3);
//        testBufferedReader();
//        System.out.println(Integer.valueOf("01"));     1
//        szfCheck();
        Calendar calendar = new GregorianCalendar(1900, 0, 0);
        Date gregorianDate = calendar.getTime();
        String formatExcelDate = format(addDay(gregorianDate, 1), "yyyy/MM/dd");
        LocalDate parse = LocalDate.parse(formatExcelDate, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        System.out.println(parse);
    }

    public static String format(Date date, String format) {
        return (new SimpleDateFormat(format)).format(date);
    }


    private static Date addDay(Date date, int day) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(5, day);
        date = calendar.getTime();
        return date;
    }

    // 身份证正则校验，简单版
    private static void szfCheck() {
        String ma = "^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\\d{4}(([1][9]\\d{2})|([2]\\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\\d{3}[0-9xX]$";
        String sfz = "123";
        System.out.println(sfz.matches(ma));
    }

    // 手机号检查
    private static void checkPhone() {
        String str = "19903000000";
        System.out.println(Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$").matcher(str).matches());
    }

    // 身份证检查
    private static void checkIDcard() {
        String num = "120221200301018914";
        boolean b = IdcardUtils.validateCard(num);
        System.out.println(b);
    }

    // 行政区划批量加入
    private static void testBufferedReader2() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\文稿啥的\\2021-1-25行政复议\\my\\all行政区划.txt")));
        String data = null;
        StringBuilder sb = new StringBuilder();
        while ((data = br.readLine()) != null) {
            sb.append(data + " ");
        }
        // 根据空格分隔
        String[] split = String.valueOf(sb).split("\\s+");
        ArrayList<Xzqh> list = new ArrayList<>();
        Xzqh xzqh = new Xzqh();
        int order = 0;
        for (String s : split) {
            // 判断一个字符串是否都为数字
            if (s.matches("[0-9]{1,}")) {
                xzqh.setCode(s);
                if ("0000".equals(s.substring(s.length() - 4))) {
                    xzqh.setLevel("1");
                    xzqh.setFather("");
                } else if ("00".equals(s.substring(s.length() - 2))) {
                    xzqh.setLevel("2");
                    xzqh.setFather(s.substring(0, s.length() - 4) + "0000");
                } else {
                    xzqh.setLevel("3");
                    xzqh.setFather(s.substring(0, s.length() - 2) + "00");
                }
            } else {
                xzqh.setName(s);
                xzqh.setOrder(order++);
                xzqh.setBh(UUID.randomUUID().toString().replaceAll("-", ""));
                list.add(xzqh);
                xzqh = new Xzqh();
            }
        }
        String sql = "INSERT INTO db_jsxzfy.t_xzqh (c_bh,c_code,c_name,c_father,c_level,n_order,n_zhxgfs,c_cjr,c_zhxgr,dt_cjsj,dt_zhxgsj) VALUES\n" +
                "\t ('*bh*','*code*','*name*','*father*','*level*','*order*',NULL,NULL,NULL,NULL,NULL);";
        for (Xzqh i : list) {
            System.out.println(sql
                    .replace("*bh*", i.getBh())
                    .replace("*code*", i.getCode())
                    .replace("*name*", i.getName())
                    .replace("*father*", i.getFather())
                    .replace("*level*", i.getLevel())
                    .replace("*order*", String.valueOf(i.getOrder()))
            );
        }

        return;
    }

    /**
     *
     * Main
     *
     * @description 用于把 word 文档中的民族单值代码转换为 insert 语句， 民族
     * @date 2021/2/3 11:21
     * @author weiZhiLin
     * @version 1.0
     */
    private static void testBufferedReader() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\文稿啥的\\2021-1-25行政复议\\my\\mzSql.txt")));
        String data = null;
        StringBuilder sb = new StringBuilder();
        while ((data = br.readLine()) != null) {
            sb.append(data + " ");
        }
        // 去除数字
        String s = String.valueOf(sb).replaceAll("\\d+", "");
        // 根据空格分隔
        String[] split = s.split("\\s+");
        List<String> list = Arrays.asList(split);
        String sql1 = "INSERT INTO db_aty.t_aty_code (c_pid,c_code,c_name,n_kwh,c_levelinfo,n_valid,n_order,c_dmjp) VALUES ('1008','";
        String sql2 = "','";
        String sql3 = "',1,NULL,1,";
        String sql4 = ",NULL);";
        int i = 1;
        StringBuilder stringBuilder = new StringBuilder();
        for (String mz : list) {
            // 格式化数字 eg：01,02...
            System.out.print(mz + ",");
            String code = String.format("%02d", i);
            stringBuilder
                    .append(sql1)
                    .append(code)
                    .append(sql2)
                    .append(mz)
                    .append(sql3)
                    .append(i)
                    .append(sql4)
                    .append("\n");
            i++;
        }
//        System.out.println(stringBuilder);
    }

    /*
     *
     * Main
     *
     * @description 键盘输入测试，用于把数据库字段名转换为驼峰命名
     * @date 2021/1/20 11:30
     * @author weizhilin
     * @version 1.0
     */
    private static void scannerTest() {
        Scanner scan = new Scanner(System.in);
        while (true) {
            String s = scan.nextLine();
            String regex = "_";
            String[] split = s.split(regex);
            if (split.length == 3) {
                s = split[1] + split[2].substring(0, 1).toUpperCase() + split[2].substring(1);
            }
            System.out.println(s);
        }
    }

    /*
     *
     * Main
     *
     * @description 利用反射，把空字段赋值为0
     * @date 2021/1/20 11:31
     * @author weizhilin
     * @version 1.0
     */
    private static void fieldTest() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Student student = new Student();
        Field[] fields = Student.class.getDeclaredFields();
        for (Field f : fields) {
            String name = f.getName();
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = Student.class.getMethod("get" + name);
            if (f.getType().equals(String.class)) {
                Method method2 = Student.class.getMethod("set" + name, String.class);
                String invoke = (String) method.invoke(student);
                System.out.println(invoke);
                String invoke1 = (String) method2.invoke(student, "666");
            }
            System.out.println(student);
        }
    }

    private static void fieldTest2() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Student student = new Student();
        Field[] fields = Student.class.getDeclaredFields();
        Field[] declaredFields = student.getStudent().getClass().getDeclaredFields();
        for (Field f : fields) {
            String name = f.getName();
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = Student.class.getMethod("get" + name);
            for (Field sf : declaredFields) {
                String sName = sf.getName();
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                Method sMethod = Student.class.getMethod("set" + name);
                if (sName.equals(name)) {
                    sMethod.invoke(student.getStudent(), method.invoke(student));
                }
            }
            System.out.println(student);
        }
    }
}
